package com.aos.iot.socket.udp.config;

import com.aos.iot.socket.common.annotation.CmdEventAnnotationScanner;
import com.aos.iot.socket.udp.SocketUdpServer;
import com.aos.iot.socket.udp.properties.SocketUdpProperties;
import com.aos.iot.socket.udp.runner.SocketUdpRunner;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = "com.aos.iot.socket.udp")
public class UdpConfiguration {

    @Bean
    public EventLoopGroup udpBossGroup() {
        return new NioEventLoopGroup();
    }

    @Bean
    public SocketUdpServer socketUdpServer(EventLoopGroup udpBossGroup,
                                           SocketUdpProperties socketUdpProperties,
                                           ChannelInitializer<? extends Channel> channelInitializer) {
        return new SocketUdpServer(udpBossGroup, channelInitializer, socketUdpProperties);
    }

    @Bean
    public CmdEventAnnotationScanner cmdEventAnnotationScanner() {
        return new CmdEventAnnotationScanner();
    }

    @Bean(
            destroyMethod = "stop"
    )
    public SocketUdpRunner socketUdpRunner(SocketUdpServer socketUdpServer) {
        return new SocketUdpRunner(socketUdpServer);
    }

}
